import { Register } from "./Register";

export class Memory {
    private data: Uint8Array;
    private init() {
        this.data = new Uint8Array(64 * 1024);
    }

    write(address: number, value: number): void {
        address &= 0xffff;
        this.data[address] = value;
    }
    //任意内存位置读取一个字节
    read(address: number): number {
        address &= 0xffff;
        return this.data[address];
    }

    //任意内存位置读取一个字
    readWord(address: number): number {
        return this.data[address++] + (this.data[address] << 8);
    }

    //零页写入一个字节
    zpgWriteByte(address: number, value: number): void {
        this.data[address & 0xff] = value;
    }

    //零页位置读取一个字节
    zpgReadByte(address: number): number {
        address &= 0xff;
        return this.data[address];
    }
    //零页位置读取一个字
    zpgReadWord(address: number): number {
        address &= 0xff;
        return this.data[address++] + (this.data[address] << 8);
    }

    reset(): void {
        for (let index in this.data) {
            this.data[index] = 0x00;
        }
    }


    constructor() {
        this.init();
    }
}